#encoding=utf-8
#!/usr/sbin
from excelword.config import email_host,send_user,email_password
import smtplib
import time
from email.mime.text import MIMEText
def send_mail(passlist,faillist):
    #统计成功个数
    passlen=len(passlist)
    faillen=len(faillist)
    alllen=int(passlen)+int(faillen)
    passrate=float(passlen)/float(alllen)
    failrate=float(faillen)/float(alllen)
    #折算为百分比
    passrate = "%.2f%%"%(passrate*100)
    failrate = "%.2f%%" % (failrate * 100)
    content = "本次共运行case"+str(alllen)+"个，其中通过"+str(passlen)+"个，失败"+str(faillen)+"个;\n"+"通过率为:"+str(passrate)+"，失败率为:"+str(failrate)+";\n"+"失败对象的行数分别是:"+str(faillist)
    filecontent = content.replace("\n","")
    # 此处将结果输出到一个文件中，方便后面统计展示使用
    with open('./rundata/rundata.log','a',encoding='utf-8') as f:
        f.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "  ")
        f.write(filecontent)
        f.write("\n")
        f.write("----------------------------\n")
    gotomail("13536810734@163.com",sub="接口测试报告",content=content)

def  gotomail(user_list,sub,content):
    #封装下信息格式
    message = MIMEText(content,_subtype="plain",_charset="utf-8")
    message['Subject']=sub
    message['From']=send_user
    message['To']=";".join(user_list)
    server = smtplib.SMTP()
    server.connect(email_host)
    server.login(send_user,email_password)
    server.sendmail(send_user,user_list,message.as_string())
    server.close()
    print("接口测试邮件发送成功")

if  __name__=="__main__":
    #测试
    send_mail([1,2,3],[4,5])



